package com.sinven.controller;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sinven.pojo.Article;
import com.sinven.pojo.Columntype;
import com.sinven.service.ArticleService;

@Controller
public class indexController {

	@Autowired
	private ArticleService articleServiceImpl;

	/**
	 * 主页面数据
	 * @param model
	 * @return
	 */
	@RequestMapping("index")
	public String SelectArticleContent(Model model,@RequestParam(value = "pageno",defaultValue = "1")int pageno,
			@RequestParam(value = "pageSize",defaultValue = "5")int pageSize) {

		try {
			List<Article> ArticleContent = articleServiceImpl.SelectArticleBypostTime();
			model.addAttribute("ArticleContent", ArticleContent);
			List<Columntype> Columntype = articleServiceImpl.SelectColumntype();
			
			model.addAttribute("columntype", Columntype);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "pages/index";

	}
	/**
	 * 文章类型查询文章
	 * @param model
	 * @param a_columnId
	 * @return
	 */
	@RequestMapping({ "indexColumntype" })
	public String acticle( @RequestParam(value = "a_columnId") int a_columnId,Model model) {
		
		try {
			List<Article> ArticleContent = articleServiceImpl.SelectArticleBypostTimeAndType(a_columnId);
			model.addAttribute("ArticleContent", ArticleContent);
			List<Columntype> Columntype = articleServiceImpl.SelectColumntype();

			model.addAttribute("columntype", Columntype);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "pages/index";

	}
	
	/**
	 * 搜索框查询文章
	 * @param model
	 * @param articleSearchText
	 * @return
	 * @throws UnsupportedEncodingException 
	 */
	@RequestMapping({ "indexArticleSearch" })
	public String indexArticleSearch(Model model, @RequestParam(value = "articleSearchText")String articleSearchText) throws UnsupportedEncodingException {
		
		articleSearchText =  new String(articleSearchText.getBytes("ISO-8859-1"),"UTF-8");
		try {
			List<Article> ArticleContent = articleServiceImpl.SelectArticleByArticleSearchText(articleSearchText);
			if(ArticleContent==null || ArticleContent.size()==0) {
				return "forward:index";
				
			}
			model.addAttribute("ArticleContent", ArticleContent);
			List<Columntype> Columntype = articleServiceImpl.SelectColumntype();
			model.addAttribute("columntype", Columntype);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return "pages/index";
		
	}

}
